package com.honey.compilation.txt;

import java.util.ArrayList;
import java.util.List;

import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.Compilation;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.types.FullyQualifiedJavaType;


/**
 * 文本文件
 * @author Administrator
 *
 */
public class TxtCompilation  implements  Compilation{
	
	/** 文件行 */
	private List<String> lines ;
	
	private List<String> afterBody ;
	
	/** 包路径和名称 */
	private FullyQualifiedJavaType type= null;
	/**
	 * 构造函数
	 */
	public TxtCompilation(FullyQualifiedJavaType type){
		lines = new ArrayList<String>();
		afterBody = new ArrayList<String>();
		this.type = type;
	}
	
	/**
	 * 构造函数
	 * @param lines 文件行
	 */
	public TxtCompilation(FullyQualifiedJavaType type,String ...lines){
		this(type);
		addline(lines);
	}
	
	/**
	 * 构造函数
	 * @param lines 添加文件内容
	 */
	public void addline(String ...lines){
		for(String line : lines){
			if(line != null){
				this.lines.add(line);
			}	
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#addFileComment(java.lang.String[])
	 */
	@Override
	public void addFileComment(String... comments) throws CompilationException {
		addline(comments);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#addAfterBodyComment(java.lang.String[])
	 */
	@Override
	public void addAfterBodyComment(String... comments) throws CompilationException {
		for(String line : comments){
			if(line != null){
				this.afterBody.add(line);
			}	
		}
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#getFileComments()
	 */
	@Override
	public List<String> getFileComments() throws CompilationException {
		return lines;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#getType()
	 */
	@Override
	public FullyQualifiedJavaType getType() {
		return type;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#compiledContent()
	 */
	@Override
	public CharacterBuilder compiledContent() throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		boolean newLine = false;
		for(String line : lines){
			if (newLine)IndentSpace.newLine(answer);else newLine = true;
			answer.append(line);
		}
		
		for(String line : afterBody){
			if (newLine)IndentSpace.newLine(answer);else newLine = true;
			answer.append(line);
		}
		return answer;
	}
}
